package com.htl.service.impl;

import com.htl.dto.BarDTO;
import com.htl.dto.BaseLineDTO;
import com.htl.entity.OrderDetail;
import com.htl.entity.OrderMaster;
import com.htl.entity.ProductInfo;
import com.htl.feign.ProductFeign;
import com.htl.form.OrderForm;
import com.htl.form.ProductForm;
import com.htl.mapper.OrderDetailMapper;
import com.htl.mapper.OrderMasterMapper;
import com.htl.service.OrderMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htl.util.EChartsColorUtil;
import com.htl.vo.BarDataVO;
import com.htl.vo.BarStyleVO;
import com.htl.vo.StackedInnerVO;
import com.htl.vo.StackedVO;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author 韩天乐
 * @since 2021-11-14
 */
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements OrderMasterService {

    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public String create(OrderForm orderForm) {
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setBuyerName(orderForm.getName());
        orderMaster.setBuyerPhone(orderForm.getPhone());
        orderMaster.setBuyerAddress(orderForm.getAddress());
        orderMaster.setBuyerOpenid(orderForm.getId());
        orderMaster.setOrderStatus(0);
        orderMaster.setPayStatus(0);
        //计算订单总价
        List<ProductForm> items = orderForm.getItems();
        BigDecimal amount = new BigDecimal(0);
        for (ProductForm item : items) {
            Integer productId = item.getProductId();
            BigDecimal price = this.productFeign.findPriceById(productId);
            BigDecimal multiply = price.multiply(new BigDecimal(item.getProductQuantity()));
            amount = amount.add(multiply);
        }
        orderMaster.setOrderAmount(amount);
        int insert = this.orderMasterMapper.insert(orderMaster);
        items = orderForm.getItems();
        for (ProductForm item : items) {
            //存储订单详情
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setProductQuantity(item.getProductQuantity());
            ProductInfo productInfo = this.productFeign.findById(item.getProductId());
            orderDetail.setProductIcon(productInfo.getProductIcon());
            orderDetail.setProductPrice(productInfo.getProductPrice());
            orderDetail.setProductName(productInfo.getProductName());
            orderDetail.setProductId(productInfo.getProductId());
            orderDetail.setOrderId(orderMaster.getOrderId());
            this.orderDetailMapper.insert(orderDetail);
        }
        //存入MQ
        this.rocketMQTemplate.convertAndSend("myTop","有新的订单");
        return orderMaster.getOrderId();
    }


    @Override
    public BarDataVO createBarData() {
        BarDataVO barDataVO = new BarDataVO();
        List<BarDTO> bar = this.orderMasterMapper.bar();
        ArrayList<String> names = new ArrayList<>();
        List<BarStyleVO> values = new ArrayList<>();
        for (BarDTO barDTO : bar) {
            names.add(barDTO.getName());
            BarStyleVO barStyleVO = new BarStyleVO();
            barStyleVO.setValue(barDTO.getValue());
            barStyleVO.setItemStyle(EChartsColorUtil.createItemStyle(barDTO.getValue()));
            values.add(barStyleVO);
        }
        barDataVO.setNames(names);
        barDataVO.setValues(values);
        return barDataVO;
    }


    @Override
    public Map<String, List> createBaseLineData() {
        List<BaseLineDTO> baseLineDTOS = this.orderMasterMapper.baseLine();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> values = new ArrayList<>();
        for (BaseLineDTO baseLineDTO : baseLineDTOS) {
            names.add(baseLineDTO.getDate());
            values.add(baseLineDTO.getValue());
        }
        Map<String,List> map = new HashMap<>();
        map.put("names", names);
        map.put("values", values);
        return map;
    }


    @Override
    public StackedVO createStackedData() {
        StackedVO stackedVO = new StackedVO();
        List<String> names = this.orderMasterMapper.findAllNames();
        List<String> dates = this.orderMasterMapper.findAllDates();
        List<StackedInnerVO> datas = new ArrayList<>();
        for (String name : names) {
            List<Integer> list = this.orderMasterMapper.findDatas(name);
            StackedInnerVO stackedInnerVO = new StackedInnerVO();
            stackedInnerVO.setName(name);
            stackedInnerVO.setData(list);
            datas.add(stackedInnerVO);
        }
        stackedVO.setNames(names);
        stackedVO.setDates(dates);
        stackedVO.setDatas(datas);
        return stackedVO;
    }

}

